﻿
using HDSmartCommunity.BUS;
using HDSmartCommunity.DAL;
using HDSmartCommunity.DevicePlatform;
using HDSmartCommunity.HDPlatform;
using HDSmartCommunity.Models;
using HDSmartCommunity.Serial;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Threading.Tasks;

namespace HDSmartCommunity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ElevatorInfoController : ControllerBase
    {
        public DeviceSerialData serial = SerialServer.serial;
        public HDZHDeviceSerialData hdzh_serial = new HDZHDeviceSerialData();
        public ElevatorInfoController()
        {
        }

        [HttpPost]
        [HttpGet]
        [Route("GetAllFloor")]
        public ResponseMessage GetAllFloor()
        {
            ResponseMessage message = new ResponseMessage();
            FloorInfoDAL da = new FloorInfoDAL();
            try
            {
                List<FloorInfo> list = da.GetAllFloor();
                message.errcode = 0;
                message.data = list;
                message.total = list.Count;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "获取楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("GetFloorByID")]
        public ResponseMessage GetFloorByID(FloorInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            FloorInfoDAL da = new FloorInfoDAL();
            try
            {
                FloorInfo list = da.GetFloorByID(info.ID);
                message.errcode = 0;
                message.data = list;
                message.total = 1;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "载入楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("UpdateFloorInfoByID")]
        public ResponseMessage UpdateFloorInfoByID(FloorInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            FloorInfoDAL da = new FloorInfoDAL();
            try
            {
                bool result = da.UpdateFloorInfoByID(info);
                if (result)
                {
                    serial.act_list.Clear();
                    serial.phy_list.Clear();
                    serial.show_list.Clear();
                    List<FloorInfo> floorInfo = serial.GetAllFloor();
                    foreach (var item in floorInfo)
                    {
                        serial.phy_list.Add(item.physicsfloor);
                        serial.show_list.Add(item.displayfloor);
                        serial.act_list.Add(item.actualfloor);
                    }
                    message.errcode = 0;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "更新楼层信息失败";
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "更新楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("InsertFloorInfo")]
        public ResponseMessage InsertFloorInfo(FloorInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            FloorInfoDAL da = new FloorInfoDAL();
            try
            {
                bool result = da.InsertFloorInfo(info);
                if (result)
                {
                    serial.act_list.Clear();
                    serial.phy_list.Clear();
                    serial.show_list.Clear();
                    List<FloorInfo> floorInfo = serial.GetAllFloor();
                    foreach (var item in floorInfo)
                    {
                        serial.phy_list.Add(item.physicsfloor);
                        serial.show_list.Add(item.displayfloor);
                        serial.act_list.Add(item.actualfloor);
                    }

                    message.errcode = 0;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "新增楼层信息失败";
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "新增楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("UpdateFloorInfo")]
        public ResponseMessage UpdateFloorInfo(List<FloorInfo> list)
        {
            ResponseMessage message = new ResponseMessage();
            string error = string.Empty;
            FloorInfoDAL da = new FloorInfoDAL();
            string ids = string.Empty;
            try
            {
                List<FloorInfo> floors = da.GetAllFloor();

                foreach (var item in list)
                {
                    if (item.ID != 0)
                    {
                        if (!da.UpdateFloorInfoByID(item))
                        {
                            error += item.physicsfloor + ",";
                        }
                        else
                        {
                            ids += item.ID + ",";
                        }
                    }
                    else
                    {
                        if (!da.InsertFloorInfo(item))
                        {
                            error += item.physicsfloor + ",";
                        }
                    }
                }
                string delete_ids = string.Empty;
                foreach (var item in floors)
                {
                    string[] arry = ids.Split(',');
                    if (!arry.Contains(item.ID.ToString()))
                    {
                        delete_ids += item.ID + ",";
                    }
                }
                if (!string.IsNullOrEmpty(delete_ids))
                {
                    delete_ids = delete_ids.Substring(0, delete_ids.Length - 1);
                    da.DeleteFloorInfo(delete_ids);
                }

                DeviceDAL deviceDAL = new DeviceDAL();
                DevInfo devInfo = deviceDAL.QueryDev();
                if (devInfo != null)
                {
                    if (devInfo.authmode == 1)
                    {
                        serial.act_list.Clear();
                        serial.phy_list.Clear();
                        serial.show_list.Clear();
                        List<FloorInfo> floorInfo_1 = da.GetAllFloor();
                        foreach (var floor_item in floorInfo_1)
                        {
                            serial.phy_list.Add(floor_item.physicsfloor);
                            serial.show_list.Add(floor_item.displayfloor);
                            serial.act_list.Add(floor_item.actualfloor);
                        }
                    }
                    else if (devInfo.authmode == 2)
                    {
                        //hdzh_serial.act_list.Clear();
                        hdzh_serial.phy_list.Clear();
                        hdzh_serial.floor_list.Clear();
                        //hdzh_serial.show_list.Clear();
                        List<FloorInfo> floorInfo_1 = da.GetAllFloor();
                        hdzh_serial.floor_list = floorInfo_1;
                        foreach (var floor_item in floorInfo_1)
                        {
                            hdzh_serial.phy_list.Add(floor_item.physicsfloor);
                            //hdzh_serial.show_list.Add(floor_item.displayfloor);
                            //hdzh_serial.act_list.Add(floor_item.actualfloor);
                        }
                    }
                }

                if (!string.IsNullOrEmpty(error))
                {
                    message.errcode = 1;
                    message.errmsg = "物理楼层" + error.TrimEnd(',') + "更新失败";
                }
                else
                {
                    message.errcode = 0;
                    message.total = 1;
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "更新楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("QueryDeviceState")]
        public ResponseMessage QueryDeviceState()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                List<HDZHCurrentElevatorStatus> list = HDZHDeviceSerialData.status_list;
                HDZHCurrentElevatorStatus point = new HDZHCurrentElevatorStatus();
                int index = list.FindIndex(x=>x.liftid==1);
                if (index>-1)
                {
                    point = list[index];
                }
                message.data = point;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "更新楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("AddCmd")]
        public ResponseMessage AddCmd(string cmd)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                HDCmdInfo hDCmdInfo = new HDCmdInfo();
                hDCmdInfo.cmd = cmd;
                hDCmdInfo.time = DateTime.Now;
                HDZHDeviceSerialData.cmd_list.Add(hDCmdInfo);
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "更新楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("UpdateMode")]
        public ResponseMessage UpdateMode(ParameterSet parameterSet)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                int reporttime;
                HDZHDeviceSerialData hDZHDeviceSerialData = new HDZHDeviceSerialData();
                ParameterRead Parameter = new ParameterRead();
                Parameter.fileServerUrl = parameterSet.fileServerUrl;
                Parameter.ntpServer = parameterSet.ntpServer;
                Parameter.httpUserName = parameterSet.httpUserName;
                Parameter.httpPassword = parameterSet.httpPassword;
                Parameter.liftCarNumber = parameterSet.liftCarNumber;
                Parameter.workMode = parameterSet.workMode;
                Parameter.lightMode = parameterSet.lightMode;
                Parameter.intervalTime = parameterSet.intervalTime;
                if (!string.IsNullOrEmpty(parameterSet.intervalTime))
                {
                    try
                    {
                        reporttime = Convert.ToInt32(parameterSet.intervalTime);
                    }
                    catch
                    {
                       
                    }
                }
                else
                {
                    reporttime = 0;
                }

                LogHelper.Log("恒大下发梯控模式" + parameterSet.workMode.ToString());
                int mode = 0;
                if (parameterSet.workMode == "1")
                {
                    mode = 1;
                    hDZHDeviceSerialData.HDLadderControl(1);
                    HDZHDeviceSerialData.TcpAuthorize = true;
                }
                else
                {
                    hDZHDeviceSerialData.HDExitControl(1);
                    HDZHDeviceSerialData.TcpAuthorize = false;
                }
                hDZHDeviceSerialData.UpdateLightMode(Convert.ToInt32(parameterSet.lightMode));
                hDZHDeviceSerialData.UpdateHdStatus(mode);

            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "更新楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("QueryFaceState")]
        public ResponseMessage QueryFaceState()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                message.data = HDZHDeviceSerialData.face_device_status;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "更新楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

    }
}
